package com.cygia.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cygia.common.Result;
import com.cygia.entity.User;
import com.cygia.mapper.UserMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    UserMapper usermapper;

    @PostMapping("/login")
    public Result<?> Login(@RequestBody User user){
        User res = usermapper.selectOne(Wrappers.<User>lambdaQuery().eq(User::getUsername, user.getUsername()).eq(User::getPassword, user.getPassword()));
        if(res==null){
            return Result.error(-1,"用户名或密码错误！");
        }
        return Result.success(0, "登陆成功!");
    }

    /**
     * 查询用户
     *
     * @param pageNum  第几页
     * @param pageSize 每页多少条
     * @param search   搜索名称
     * @return
     */
    @GetMapping("/pageUser")
    public Result<?> findPageUser(@RequestParam(defaultValue = "1") Integer pageNum,
                                  @RequestParam(defaultValue = "10") Integer pageSize,
                                  @RequestParam(defaultValue = "") String search) {
        LambdaQueryWrapper<User> wrapper = Wrappers.<User>lambdaQuery();
        if (!search.isEmpty() || search.length() > 0) {
            wrapper.like(User::getUsername, search);
        }
        IPage page = usermapper.selectPage(new Page(pageNum, pageSize), wrapper);

        List records = page.getRecords();
        System.out.println("--共"+page.getTotal()+"条\n--每页"+page.getSize()+"条\n--第"+page.getCurrent()+"页\n--查询到的数据：\n");
        for (int i= 0;i<records.size();i++) {
            System.out.println(records.get(i));
        }
        return Result.success(page);
    }

    /**
     * 添加or更新用户
     *
     * @param user
     * @return
     */
    @PostMapping("/saveUser")
    public Result<?> saveUser(@RequestBody User user) {
        if (user == null) {
            return Result.error(-1, "创建用户不存在");
        }

        if(user.getPassword() == null){
            user.setPassword("123456");
        }
        if (user.getId() == null || user.getId().equals("")) {
            User res = usermapper.selectOne(Wrappers.<User>lambdaQuery().eq(User::getUsername, user.getUsername()));
            if(res==null){
                usermapper.insert(user);
                return Result.success(0, "添加用户成功!");
            }else{
                return Result.success(-1, "用户名已存在!");
            }
        } else {
            usermapper.updateById(user);
            return Result.success(1, "更新用户成功!");
        }
    }


    /**
     * 删除用户
     *
     * @param id
     * @return
     */
    @DeleteMapping("/delUser/{id}")
    public Result<?> delUser(@PathVariable Integer id) {
        if (id != null) {
            usermapper.deleteById(id);
            return Result.success(3, "删除用户成功！");
        } else {
            return Result.success(4, "删除用户失败！");
        }
    }

}
